/**
 * 
 */
package com.asdan.seededu.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.asdan.seededu.entity.security.Principal;
import com.asdan.seededu.entity.security.School;
import com.asdan.seededu.entity.security.Student;
import com.asdan.seededu.entity.security.SubProject;
import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.service.UserService;
import com.asdan.seededu.util.DatabaseAccess;


/**
 * @author lute
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	
	/* (non-Javadoc)
	 * @see com.demo2do.alaska.service.UserService#getPrincipal(java.lang.String)
	 */

	public User getUser(String email , int ID){
		
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		User stu = null ;
		String sql = "select uid  , name , email , isok , validateCode  from user  where (email =? or uid = ?) AND ifdelete = 0  ";
		System.out.println(sql);
		try{
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql);
			pstmt.setString(1, email);
			pstmt.setInt(2,ID ) ;
			rs = pstmt.executeQuery();
			while(rs.next()){
				stu= new User();
  				stu.setUid(rs.getInt("UID")) ;
				stu.setName(rs.getString("NAME")) ;
				stu.setEmail(rs.getString("EMAIL")) ;
				stu.setIsok(rs.getInt("isok")) ;
				stu.setValidateCode(rs.getString("validateCode")) ;
			}
			
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			try{
				pstmt.close();
				cn.close();
				databaseAccess.close() ;
			}catch(Exception ex){
					pstmt = null ;
					cn = null ;
			}
		}
		return stu ;
	}
	public boolean updateUser(User user) {
		boolean temp = true ;
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		String sql = "update user  set  isok =?  where UID= ?" ;
		System.out.println("user.getUid() "+user.getUid());
		System.out.println(sql) ;
		try {
			cn = databaseAccess.getConn() ;
			pstmt = cn.prepareStatement(sql) ;
			pstmt.setInt(1,user.getIsok()) ;
			pstmt.setInt( 2,user.getUid()) ;
			pstmt.executeUpdate() ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			temp = false ;
			e.printStackTrace();
		}finally{
			try {
				pstmt.close() ;
				cn.close() ;
				databaseAccess.close() ;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
		return temp;
	}

}
